using System.Collections;
using System.Linq;
using ArticleManagementSystem.Api.Entity;
using ArticleManagementSystem.Api.Params;
using ArticleManagementSystem.Api.Repostisory;
using Microsoft.AspNetCore.Mvc;

namespace ArticleManagementSystem.Api.Controllers
{
    [ApiController]
    [Route("[Controller]")]
    public class CommentController : ControllerBase
    {
        private readonly IRepostisory<Comment> _commentRepostisory;
        private readonly IRepostisory<Users> _usersRepostisory;
        public CommentController(IRepostisory<Users> usersRepostisory, IRepostisory<Comment> commentRepostisory)
        {
            _commentRepostisory = commentRepostisory;
            _usersRepostisory = usersRepostisory;
        }
        /// <summary>
        /// 发布评论
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [HttpPost, Route("toComment")]
        public dynamic CommentMsg(ComMsg msg)
        {
            //文章id
            var ArticleId = msg.ArticleId;
            //用户id
            var UserId = msg.UserId;
            //对评论id
            var ToCommentId = msg.ToCommentId;
            //评论内容   
            var Content = msg.Content;
            var res = new Comment
            {
                ArticleId = ArticleId,
                FromUserId = UserId,
                ToCommentId = ToCommentId,
                Content = Content
            };
            _commentRepostisory.Insert(res);
            return new
            {
                code = 200,
                data = res,
                msg = "评论成功"
            };
        }

        //获取对应评论
        [HttpGet, Route("{id}/getComment")]
        public dynamic GetCommentMsg(int id)
        {

            var ArticleId = id;
            var CommentTable = _commentRepostisory.Table;
            var ArticleComment = CommentTable.Where(x => x.ArticleId == ArticleId).ToList();
            var users = _usersRepostisory.Table;

            ArrayList list = new ArrayList();
            foreach (var item in ArticleComment)
            {
                var user = users.Where(x => x.Id == item.FromUserId).FirstOrDefault();
                var comment = CommentTable.Where(x => x.Id == item.ToCommentId).FirstOrDefault();
                var ArticleComments = CommentTable.Where(x => x.Id == item.Id).FirstOrDefault();
                if (comment != null)
                {
                    var toUser = users.Where(x => x.Id == comment.FromUserId).FirstOrDefault();
                    var tep = new
                    {    
                        Id = ArticleComments.Id,
                        CommentFormUsername = user.Username,
                        CommentConent = item.Content,
                        ToCommentId = item.ToCommentId,
                        CommentToUsername = toUser != null ? toUser.Username : ""
                    };
                    list.Add(tep);
                }
                else
                {
                    var tep = new
                    {
                        Id = ArticleComments.Id,
                        CommentFormUsername = user.Username,
                        CommentConent = item.Content,
                        ToCommentId = item.ToCommentId,
                        CommentToUsername = ""
                    };
                    list.Add(tep);
                }

            }

            return new
            {
                code = 200,
                data = list,
                mag = "获取成功"
            };
        }
    }
}